/**
 * <p>Copyright (c) 2017 深圳市鹏途交通科技有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	         </p>
 * <p>创建者   :	lei 
 * 
 * <p>描   述  :   TrainingController.java for com.pengtu.gsj.controller.yh    </p>
 * 
 * <p>最后修改 : $: 2017年6月5日-下午2:38:13 v 1.0.0	 lei   $ </p>
 * 
*/

package com.pengtu.gsj.controller.yh;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pengtu.gsj.controller.base.DefaultBaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.yh.Training;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.yh.TrainingService;

/**
 * 
 * TrainingController
 * 
 * 2017年6月5日 下午2:38:13
 * 
 * @version 1.0.0
 * 
 */
@Controller
@RequestMapping("train")
public class TrainingController extends DefaultBaseController{

	@Autowired
	private TrainingService trainingService;
	
	@ModelAttribute
	public Training loadEntity(String id) {
		Training training = null;
		if (StringUtils.isNotEmpty(id)) {
			training = trainingService.getTraining(id);
		} else {
			training = new Training();
		}
		return training;
	}
	
	
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showLisfInfo() {
		return "yh/train_list";
	}
	
	@RequestMapping(value="list", produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<Training> getTrainingList(Page<Training> page, HttpServletRequest request){
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("createDate");
			page.setOrder(Page.DESC);
		}
		page = trainingService.findTrainings(page, queryFilter);
		return page;
	}
	
	@RequestMapping(value = "save", method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public boolean saveTraining(@ModelAttribute Training training) {
		trainingService.saveTraining(training);
		return true;
	}
	
	@RequestMapping(value="show")
	@ResponseBody                                                     
	public Training showTraining (@ModelAttribute Training training){
		return training;
	}
	
	@RequestMapping(value="delete")
	@ResponseBody
	public boolean deleteTraining(@ModelAttribute Training training){
		trainingService.deleteTraining(training);
		return true;
	}

}
